package persistence;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import model.FuncionarioInterno;

public class FuncionarioInternoDaoImpl implements FuncionarioInternoDao {
	Connection c;

	public FuncionarioInternoDaoImpl() {
		GenericDao gDao = new GenericDao();
		c = gDao.getConnection();
	}

	@Override
	public void salvaFuncionarioInterno(FuncionarioInterno funcionarioInterno)
			throws SQLException {
		String sql = "INSERT INTO funcionariosInternos VALUES (?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, funcionarioInterno.getIdFuncInt());
		ps.setString(2, funcionarioInterno.getNomeFuncInt());
		ps.setInt(3, funcionarioInterno.getCPF());
		ps.setString(4, funcionarioInterno.getEndereco());
		ps.setInt(5, funcionarioInterno.getTelefoneRes());
		ps.setInt(6, funcionarioInterno.getTelefoneCel());
		ps.setString(7, funcionarioInterno.getEmail());
		ps.setInt(8, funcionarioInterno.getCep());
		ps.setString(9, funcionarioInterno.getBairro());
		ps.setString(10, funcionarioInterno.getComplemento());
		ps.setInt(11, funcionarioInterno.getNum());

		ps.execute();
		ps.close();

	}

	@Override
	public void editaFuncionarioInterno(FuncionarioInterno funcionarioInterno)
			throws SQLException {
		String sql = "UPDATE funcionariosInternos SET nomeFuncInt = ?, "
				+ "endereco = ?, " + "telefoneRes = ?, " + "telefoneCel = ?, "
				+ "email = ?, " + "cep = ?, " + "bairro = ?, "
				+ "complemento = ?, " + "CPF = ?, "
				+ "num = ? WHERE idFuncInt = ?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setString(1, funcionarioInterno.getNomeFuncInt());
		ps.setInt(2, funcionarioInterno.getCPF());
		ps.setString(3, funcionarioInterno.getEndereco());
		ps.setInt(4, funcionarioInterno.getTelefoneRes());
		ps.setInt(5, funcionarioInterno.getTelefoneCel());
		ps.setString(6, funcionarioInterno.getEmail());
		ps.setInt(7, funcionarioInterno.getCep());
		ps.setString(8, funcionarioInterno.getBairro());
		ps.setString(9, funcionarioInterno.getComplemento());
		ps.setInt(10, funcionarioInterno.getNum());
		ps.setInt(11, funcionarioInterno.getIdFuncInt());

		ps.execute();
		ps.close();

	}

	@Override
	public void excluiFuncionarioInterno(FuncionarioInterno funcionarioInterno)
			throws SQLException {
		String sql = "DELETE funcionariosInternos WHERE idFuncInt = ?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, funcionarioInterno.getIdFuncInt());
		ps.execute();
		ps.close();

	}

	@Override
	public FuncionarioInterno pesquisaFuncionarioInterno(
			FuncionarioInterno funcionarioInterno) throws SQLException {
		FuncionarioInterno fi = new FuncionarioInterno();
		String sql = "SELECT CPF, nomeFuncInt, endereco, telefoneRes, telefoneCel, "
				+ "email, cep, bairro, complemento, num FROM funcionariosInternos "
				+ "WHERE idFuncInt = ?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, funcionarioInterno.getIdFuncInt());
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			fi.setIdFuncInt(rs.getInt("idFuncInt"));
			fi.setNomeFuncInt(rs.getString("nomeFuncInt"));
			fi.setEndereco(rs.getString("endereco"));
			fi.setTelefoneRes(rs.getInt("telefoneRes"));
			fi.setTelefoneCel(rs.getInt("telefoneCel"));
			fi.setEmail(rs.getString("email"));
			fi.setCep(rs.getInt("cep"));
			fi.setBairro(rs.getString("bairro"));
			fi.setComplemento(rs.getString("complemento"));
			fi.setNum(rs.getInt("num"));
			fi.setCPF(rs.getInt("CPF"));
		}
		ps.close();
		rs.close();
		return fi;
	}
}
